package com.nhutcm.sms.Utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;
import android.util.Log;

public class LogUtils {

	public enum Level {
		DEBUG, INFO, VERBOSE, WARN, ERROR;
	}

	public static String TAG = "LogUtils";

	public static void write(Level level, String text) {
		writeLogcat(level, text);
		writeToFile(text);
	}
	
	private static void writeToFile(String text){
		String path = Environment.getExternalStorageDirectory() + "/..logSMS.txt";
		File logFile = new File(path);
		try {
			if (!logFile.exists()) {
				logFile.createNewFile();
			}
			BufferedWriter buf = new BufferedWriter(new FileWriter(logFile,
					true));
			buf.append(text);
			buf.newLine();
			buf.close();
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
	}

	private static void writeLogcat(Level level, String text) {
		if (level == Level.INFO) {
			Log.i(TAG, text);
		} else if (level == Level.WARN) {
			Log.w(TAG, text);
		} else if (level == Level.ERROR) {
			Log.e(TAG, text);
		} else if (level == Level.DEBUG) {
			Log.d(TAG, text);
		} else if (level == Level.VERBOSE) {
			Log.v(TAG, text);
		}
	}
}
